##################################################################
# File : /lua/maui/bitmap.lua
# Description : Bitmap control
# Author : GPG
##################################################################

local Control = import('control.lua').Control

Bitmap = Class(moho.bitmap_methods, Control) {
	--[[
	Methods:
		SetTexture(filename(s), border=1)
		SetSolidColor(color)
		SetUV(float u0, float v0, float u1, float v1)
		SetTiled(bool)
		UseAlphaHitTest(bool)
		Loop(bool)
		Play()
		Stop()
		SetFrame(int)
		int GetFrame()
		int GetNumFrames()
		SetFrameRate(float fps)
		SetFramePattern(pattern) 
			# pattern is an array of integers reflecting texture indicies
			# Frame patterns are arrays that indicate what texture to play at a particular frame index
			# Textures are indexed by the order you pass them in to SetTexture. Note that frames are
			# 0 based, not 1 based.
		SetForwardPattern()
		SetBackwardPattern()
		SetPingPongPattern()
		SetLoopPingPongPattern()
		ShareTextures(bitmap) 
			# allows two bitmaps to share the same textures
		SetVerticies takes 4 verts and will allow you to draw the bitmap without the constraint of being
		rectangular. It will ignore all dimensions set for the control and the verts are not lazy vars
		so you'll need to update them manually (or via another lazy var's update function.) Note that there
		are some restrictions, for example, you can't use UV's to tile the texture since the bitmap is 
		actually showing a piece of a composited texture. Also note, until you call ClearVerticies the
		verticies will be used, not the control coordinates.
		the table format for a vertex is:
		{
		   pos = {x,y,z},
		   uv = {u,v},
		   color = scrColor
		}
		SetVerticies(vert1, vert2, vert3, vert4)
		ClearVerticies()
	--]]
	
    __init = function(self, parent, filename, debugname)
        InternalCreateBitmap(self, parent)
        if debugname then
            self:SetName(debugname)
        end

        local LazyVar = import('/lua/lazyvar.lua')
        self._filename = {_texture = LazyVar.Create(), _border = 1}
        self._filename._texture.OnDirty = function(var)
            if not IsDestroyed(self) then
                self:SetNewTexture(self._filename._texture(), self._filename._border)
            end
        end

        if filename then
            self:SetTexture(filename)
        end
    end,

    SetTexture = function(self, texture, border)
        if self._filename then
            # replace any '//' with single '/'
            if type(texture) == "string" then
                texture = texture:gsub("/+", "/")
            end
                 
            texTestname = texture, border        
                    
            if texTestname != self.texName then
                border = border or 1
                self._filename._border = border
                self._filename._texture:Set(texture)
                self.texName = texTestname
            end    
        end
    end,
    
    ResetLayout = function(self)
        Control.ResetLayout(self)
        self.Width:SetFunction(function() return self.BitmapWidth() end)
        self.Height:SetFunction(function() return self.BitmapHeight() end)
    end,

    OnInit = function(self)
        Control.OnInit(self)
    end,
    
    OnDestroy = function(self)
        if self._filename and self._filename._texture then
            self._filename._texture:Destroy()
        end
        self._filename = nil
    end,
    
    # callback scripts
    OnAnimationFinished = function(self) end,
    OnAnimationStopped = function(self) end,
    OnAnimationFrame = function(self, frameNumber) end,
    
    VertexColors = function(self, c1, c2, c3, c4)
        local v1 = {
            pos = {self.Left(), self.Top(), 0.0},
            uv = {1.0, 1.0},
            color = c1,
        }
		LOG(repr(v1))
        local v2 = {
            pos = {self.Right(), self.Top(), 0.0},
            uv = {1.0, 1.0},
            color = c2,
        }
        local v3 = {
            pos = {self.Right(), self.Bottom(), 0.0},
            uv = {1.0, 1.0},
            color = c3,
        }
        local v4 = {
            pos = {self.Left(), self.Bottom(), 0.0},
            uv = {1.0, 1.0},
            color = c4,
        }
        self:SetVerticies(v1, v2, v3, v4)
        
    end,
}

